<template>
  <div>
    <basic-container>
      <div class="app-container">
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">
          <el-form-item label="账户名称">
            <el-input v-model="queryParams.account" placeholder="请输入角色名称" clearable @clear="getList" />
          </el-form-item>
          <el-form-item label="用户名称">
            <el-input v-model="queryParams.name" placeholder="请输入角色名称" clearable @clear="getList" />
          </el-form-item>
          <el-form-item label="用户电话">
            <el-input v-model="queryParams.phone" placeholder="请输入角色名称" clearable @clear="getList" />
          </el-form-item>
          <el-form-item label="是否禁用">
            <el-select v-model="queryParams.enable" placeholder="请选择" clearable @clear="getList">
              <el-option v-for="item in dataMuen" :key="item.value" :label="item.label" :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd">新增</el-button>
          </el-col>
        </el-row>
        <el-table v-loading="loading" :data="menuList" row-key="user_id" :default-expand-all="isExpandAll"
          :tree-props="{ children: 'children', hasChildren: 'hasChildren' }">

          <el-table-column prop="name" label="用户名称"></el-table-column>
          <el-table-column prop="account" label="账户名称" align="center"></el-table-column>
          <el-table-column prop="describe" label="角色描述" align="center"></el-table-column>
          <el-table-column prop="phone" label="电话" align="center">
            <template slot-scope="scope">
              {{ scope.row.phone == null ? '未添加' : scope.row.phone }}
            </template>
          </el-table-column>
          <el-table-column prop="framework" label="人员组织架构" :show-overflow-tooltip="true" align="center"></el-table-column>
          <el-table-column prop="time" label="创建时间" :show-overflow-tooltip="true" align="center"></el-table-column>
          <el-table-column prop="modify" label="修改时间" :show-overflow-tooltip="true" align="center"></el-table-column>
          <el-table-column prop="modify" label="状态" :show-overflow-tooltip="true" align="center">
            <template slot-scope="scope">
              {{ scope.row.enable == 1 ? '正常' : '禁用' }}
            </template>
          </el-table-column>
          <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
            <template slot-scope="scope">
              <el-button size="mini" type="text" icon="el-icon-edit" :disabled="scope.row.account == 'admin'"
                @click="handleUpdate(scope.row)">修改</el-button>
              <el-button v-if="scope.row.enable == 1" size="mini" :disabled="scope.row.account == 'admin'" type="text"
                icon="el-icon-warning-outline" @click="handleDelete(scope.row, 1)"> 禁用</el-button>
              <el-button v-else size="mini" type="text" icon="el-icon-warning-outline" @click="handleDelete(scope.row, 2)"
                style="color: red;">启用</el-button>
            </template>

          </el-table-column>
        </el-table>
        <pagination v-show="total > 0" :total="total" :page.sync="queryParams.index" :limit.sync="queryParams.size"
          @pagination="getList" />
        <!-- 添加或修改菜单对话框 -->
        <el-dialog :title="title" :visible.sync="open" width="680px" append-to-body :close-on-click-modal="false">
          <el-form ref="form" :model="form" :rules="rules" label-width="120px">
            <el-row>
              <el-col :span="19">
                <el-form-item label="账户名称" prop="account">
                  <el-input v-model="form.account" placeholder="请输入账户名称" />
                </el-form-item>
              </el-col>
              <el-col :span="19">
                <el-form-item label="账户密码" prop="password">
                  <el-input v-model="form.password" placeholder="请输入账户密码" />
                </el-form-item>
              </el-col>
              <el-col :span="19">
                <el-form-item label="添加角色" prop="role_id">
                  <el-select v-model="form.role_id" clearable placeholder="请选择" style="width: 100%">
                    <el-option v-for="item in roleOptions" :key="item.role_id"
                      :label="item.role + '  (' + item.describe + ')'" :value="item.role_id">
                    </el-option>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="19">
                <el-form-item label="用户名称" prop="name">
                  <el-input v-model="form.name" placeholder="请输入用户名称" />
                </el-form-item>
              </el-col>
              <el-col :span="19">
                <el-form-item label="人员组织架构" prop="framework">
                  <el-input v-model="form.framework" placeholder="请输入组织架构" />
                </el-form-item>
              </el-col>
              <el-col :span="19">
                <el-form-item label="用户电话" prop="phone">
                  <el-input v-model="form.phone" @input="onPhoneInput" placeholder="请输入电话" />
                </el-form-item>
              </el-col>
              <el-col :span="19">
                <el-form-item label="是否禁用">
                  <el-select v-model="form.enable" placeholder="请选择" style="width: 100%">
                    <el-option v-for="item in dataMuen" :key="item.value" :label="item.label" :value="item.value">
                    </el-option>
                  </el-select>
                </el-form-item>
              </el-col>
            </el-row>
          </el-form>
          <div slot="footer" class="dialog-footer">
            <el-button type="primary" @click="submitForm">确 定</el-button>
            <el-button @click="cancel">取 消</el-button>
          </div>
        </el-dialog>
      </div>
    </basic-container>
  </div>
</template>
  
<script>
import { adduser, getUserlist, rolesAll, eidtUsers } from "@/api/userlist";
import { mapGetters } from "vuex";

export default {
  data() {

    return {
      // 遮罩层
      propObj: {
        multiple: true,
        value: 'mark',
        label: 'label',
        children: 'children'
      },
      loading: false,
      // 显示搜索条件
      showSearch: true,
      value1: [],
      total: 1,
      inMess: [],
      menuList: [],
      roleOptions: [],
      dataMuen: [{
        value: 0,
        label: '是'
      }, {
        value: 1,
        label: '否'
      },],
      options: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否展开，默认全部折叠
      isExpandAll: false,
      // 重新渲染表格状态
      refreshTable: true,
      // 查询参数
      queryParams: {
        index: 1,
        enable: null,
        size: 10,
        account: '',
        name: '',
        phone: '',
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        account: [
          { required: true, message: "账户名称不能为空", trigger: "blur" }
        ],
        password: [
          { required: true, message: "账户密码不能为空", trigger: "blur" }
        ],
        role_id: [
          { required: true, message: "角色不能为空", trigger: "blur" }
        ],
        name: [
          { required: true, message: "用户名称不能为空", trigger: "blur" }
        ],
        framework: [
          { required: true, message: "人员组织架构不能为空", trigger: "blur" }
        ]
      }
    }

  },
  created() {
    this.getList();
    this.gitrAllrole()
  },
  computed: {
    ...mapGetters(["userInfo", "menu"]),
  },

  methods: {
    // 匹配电话号码
    onPhoneInput(event) {
      let value = event
      // 使用正则表达式过滤非数字字符
      value = value.replace(/\D/g, '');
      // 更新输入框的值
      this.form.phone = value;
    },
    /** 查询菜单列表 */
    getList() {
      this.loading = true;
      getUserlist(this.queryParams).then(response => {
        this.menuList = response.data.data
        this.total = response.data.count;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.getList();
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        describe: null,
        enable: 1,
        phone: "",
        role_id: null,
        framework: null,
        name: null,
        password: null,
        account: null,
        user_id: null,
        role: null,
      };

    },
    /** 搜索按钮操作 */
    handleQuery() {
      if (this.queryParams.phone !== '') {
        this.menuList = this.menuList.filter(item => {
          // 在使用includes之前检查item.phone是否不为null或undefined
          return item.phone && item.phone.includes(this.queryParams.phone);
        });
      } else if (this.queryParams.name !== '') {
        this.menuList = this.menuList.filter(item => {
          // 在使用includes之前检查item.name是否不为null或undefined
          return item.name && item.name.toLowerCase().includes(this.queryParams.name.toLowerCase());
        });
      } else {
        this.getList();
      }
    },

    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams = {
        index: 1,
        enable: null,
        size: 10,
        account: '',
        name: '',
        phone: '',
      }
      this.getList();

    },
    /** 新增按钮操作 */
    handleAdd() {
      this.open = true;
      this.reset()
      this.title = "新增用户";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      console.log(row);
      this.reset()
      this.form = row
      this.open = true;
      this.title = "修改用户";
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.title == "新增用户") {
            adduser(this.form).then(() => {
              this.$message({
                message: '添加成功',
                type: 'success'
              });
              this.open = false;
              this.getList();
              this.reset();
            })
          } else {
            eidtUsers(this.form).then(() => {
              this.$message({
                message: '修改成功',
                type: 'success'
              });
              this.open = false;
              this.getList();
              this.reset();
            })
          }
        }
      });
    },
    // 获取全部角色
    gitrAllrole() {
      rolesAll().then(res => {
        this.roleOptions = res.data.data
      })
    },
    /** 删除按钮操作 */
    handleDelete(row, index) {
      console.log(row);
      if (index == 1) {
        this.$confirm('是否确认禁用账户名称为"' + row.account + '"的用户？').then(() => {
          this.form = row
          this.form.enable = 0
          eidtUsers(this.form).then(() => {
            this.$message({
              message: '禁用成功',
              type: 'success'
            });
            this.open = false;
            this.getList();
            this.reset();
          })
        }).catch(() => { });
      } else {
        this.$confirm('是否确认启用账户名称为"' + row.account + '"的用户？').then(() => {
          this.form = row
          this.form.enable = 1
          eidtUsers(this.form).then(() => {
            this.$message({
              message: '启用成功',
              type: 'success'
            });
            this.open = false;
            this.getList();
            this.reset();
          })
        }).catch(() => { });
      }
    }
  }
};
</script>
  
<style></style>
  